<!-- 通用编辑功能模板 -->
<template>
  <div class="coverBG" v-show="show">
    <div class="floatPanel" v-show="show">
      <div class="title">
        <div class="name">{{menuName}}</div>
        <div class="butClose" @click="closePanel"></div>
      </div>
      <div class="body">
        <div style="width: 100%;clear: both;" v-for="item in paramData">
          <field-operate v-model="item.value" :theData="item" :theOrg="theOrg" />
        </div>
      </div>
      <div class="button">
        <button class="butOne" @click="submitData">确定</button>
        <button class="butTwo" @click="closePanel">取消</button>
      </div>
    </div>
  </div>
</template>
<script>
import fieldOperate from "./fieldOperate.vue";
export default {
  data() {
    return {
      menuName: "",
      dataKey: "",
      submitUrl:'',
      paramData:[],
      tableData:{},
      dataId: 0
    };
  },
  components:{
    fieldOperate
  },
  props:{
    theData:{
      type:Object,
      required:true
    },
    theOrg:{
      type:Number,
      required:true
    },
    show:{
      type:Boolean,
      required:false
    }
  },
  created() {
    
  },
  watch: {
    theData(data){
      this.dataId = this.theData.dataId;
      this.orgId = this.theOrg;
      this.paramData = {};
      this.tableData = {};
      this.submitUrl = '';
      //this.loadData();
    }
  },
  methods: {
    closePanel(){
      this.show = false;
    },
    async loadData(){
      var toUrl = this.changeUrl(this.theData.actionUrl+'&id='+this.dataId);
      let { data: res } = await this.$http.get(toUrl);
      if (res.result) {
        console.log('get type data:>>');
        console.log(res.obj);
        this.paramData = res.formParam;
        this.menuName = res.obj.menuName;
        this.dataKey = res.obj.dataKey;
        this.submitUrl = res.obj.submitUrl;
        if(this.dataId != null && this.dataId > 0){
          for(item in this.paramData){
            this.tableData[item.paramName] = item.value;
          }
        }
      }else{
        this.clewMsg(res.type,res.mes);
      }
    },
    async submitData(){
      let verifyItems = this.paramData.filter(x => x.allow && x.null);
      var verify = true;
      var message = '';
      console.log('verify data:>>');
      console.log(verifyItems);
      if (verifyItems != null && verifyItems.length > 0) {
        for(item in verifyItems){
          var theVal = this.tableData[item.paramName];
          if(item.type == 'int' || item.type == 'float' || item.type.toLowerCase().includes("select")){            
            if(theVal == null || theVal.trim().length <= 0 || theVal <= 0){
              message += '#'+item.name+'#，为必填项，请确认填写;<br/>';
              verify = false;
            }
          }else{
            if(theVal == null || theVal.trim().length <= 0) {
              message += '#'+item.name+'#，为必填项，请确认填写;<br/>';
              verify = false;
            }
          }
        }
      }
      if(verify){
        var postUrl = this.submitUrl;
        this.tableData['id'] = this.dataId;
        this.tableData['orgId'] = this.theOrg;
        console.log('type save data:>>');
        console.log(this.tableData);
        let { data: res } = await this.$http.post(postUrl, this.tableData);
        if (res.result) {
          this.clewMsg(1,'操作成功');
          this.dataId = res.dataId;
        } else {
          this.clewMsg(res.type,res.mes);
        }
      }else{
        this.clewMsg(2,message);
      }
    },
    //字段组件修改值
    updateChangeVal(data){
      console.log('update data:>>');
      console.log(data);
      this.tableData[data.param] = data.value;
    },
    //处理URL自增加组织ID
    changeUrl(url){
      if(/\?|&/.test(url)){
        url += '&orgId=' + this.theOrg;
      }else{
        url += '?orgId=' + this.theOrg;
      }      
      console.log("changeURL:" + url);
      return url;
    },
    //提款信息
    clewMsg(type,mes){
      var theType = 'warning';
      if(type == 1)
        theType = 'success';
      else if(type == 3)
        theType = 'error';
      this.$message({
        message: mes,
        type: theType
      });
    }
  }
};
</script>
<style scoped>
  .floatPanel{
    padding: fixed;
    z-index: 999;
    left: calc((100% - 620px) / 2);
    top: 100px;
    width: 620px;
    height: 460px;
    background-color: #FFF;
    border-radius: 8px;
    box-shadow: 0 1px 3px 2px rgba(0,0,0,0.15);
  }
  .coverBG{
    position: fixed;
    z-index: 998;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.15);
    cursor: default;
  }
  .title{
    clear: both;
    width: calc(100% - 50px);
    height: 40px;
    padding: 10px 25px;
    border-bottom: solid 1px #EEE;
  }
  .name{
    float: left;
    height: 40px;
    line-height: 40px;
    color: #000;
    font-size: 16px;
    text-align: left;
  }
  .butClose{
    float: right;
    height: 40px;
    width: 40px;
    cursor: pointer;
    background: url(../assets/img/close.png) no-repeat;
    background-size: 50% 50%;
    background-position: center center;
  }  
  .body{
    clear: both;
    padding: 20px 20px 40px 20px;
    width: calc(100% - 40px);
    height: calc(100% - 150px);
    overflow-x: hidden;
    overflow-y: auto;
  }
  .body::-webkit-scrollbar {  
    display: none; 
    width: 0px;
  }
  .table{
    float: left;
    width: 50%;
  }
  .button{
    clear: both;
    width: calc(100% - 20px);
    height: 40px;
    padding: 10px;
    border-top: solid 1px #EEE;
    align-items: right;
    text-align: right;
  }
  .butOne{
    padding: 8px 15px;
    height: 22px;
    line-height: 22px;
    margin-left: 10px;
    letter-spacing: 5px;
    font-size: 16px;
    color: #FFF;
    background: #00A578;
    border: 1px solid #00A578;
    border-radius: 8px;
    cursor: pointer;
    text-align: center;
  }
  .butTwo{
    padding: 8px 15px;
    height: 22px;
    line-height: 22px;
    margin-left: 10px;
    letter-spacing: 5px;
    font-size: 16px;
    color: #000;
    background: #FFF;
    border: 1px solid #888;
    border-radius: 8px;
    cursor: pointer;
    text-align: center;
  }
  .butTwo:hover{
    color: #00A578;
    border-color: #00A578;
  }
</style>
